import static java.lang.Thread.sleep;

class MyThread1 extends Thread {
    public void run() {
        // 线程执行的代码
        for (int i = 0; i < 3; i++) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("通过继承Thread类创建线程");
        }
    }
}

class MyThread2 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("通过实现runnable创建线程");
        }
    }
}

public class Test {
    public static void main(String[] args) {
        /*MyThread1 t1 = new MyThread1();
        t1.start(); // 启动线程

        for (int i = 0; i < 3; i++) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Test运行");
        }*/

        /*MyThread2 t2=new MyThread2();
        Thread t=new Thread(t2);
        t.start();
        for (int i = 0; i < 3; i++) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Test运行");
        }*/

       /* new Thread(){
            @Override
            public void run() {
                System.out.println("继承 Thread 类并重写 run 方法，使用匿名内部类创建线程");
            }
        }.start();
    }*/

       /* new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("实现 Runnable 接口并重写 run 方法，使用匿名内部类创建线程");
                }
            }
        }).start();
        for (int i = 0; i < 3; i++) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Test运行");
        }*/

        new Thread(() ->{
            for (int i = 0; i < 3; i++) {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("使用 Lambda 表达式创建线程");
            }
        } ).start();
        for (int i = 0; i < 3; i++) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Test运行");
        }

    }
}
